﻿/// <reference path="../../Scripts/commen_js/MJ_JSComment.js" />
/// <reference path="../../Scripts/commen_js/jquery.cookie.min.js" />
/// <reference path="../../Scripts/commen_js/jquery-2.0.2.min.js" />
var OrderPage = {
    BindBata: function () {
        var openid = $.cookie("openid");
        if (isUndefinedNullOrEmpty(openid)) {
            alert("服务器获取数据失败请稍候再试");
            return false;
        }
        poststr = "{\"type\":\"orderpage\",\"htmldata\":\""+ openid + "\"}";
        this.postajaxs(poststr, this.showdata);
    },
    change_color: 1,
    sendid:1,
    sendtype:1,//发送的修改的类型1是确认收货，0是删除

    showdata: function (data) {
        if (data.result == "ok") {
            var tabledata = data.table;
            var tableactive_ = document.getElementById("tablebody");
            $("#tablebody").empty();
            if (tabledata.length > 0) {
                $("#goes").hide();
                for (var i = 0; i < tabledata.length; i++) {
                        var tr = document.createElement("tr");
                        if (OrderPage.change_color % 2 == 0) {
                            tr.className = "bg-info";
                            OrderPage.change_color++;
                        }
                        else {
                            OrderPage.change_color++;
                        }
                        var td = document.createElement("td");
                        var img = document.createElement("img");
                        img.src = "../" + tabledata[i].childimg;
                        td.appendChild(img);
                        $("#user").text("尊敬的"+$.trim(tabledata[0].phone)+"用户");
                        var td1 = document.createElement("td");
                        td1.style.textAlign = "left";
                        var div = document.createElement("div");
                        div.className = "media-heading";
                        div.innerHTML =$.trim(tabledata[i].product_name);
                        var div1 = document.createElement("div");
                        div1.innerHTML ="价格："+$.trim(tabledata[i].price);
                        var div2 = document.createElement("div");
                        var finish=$.trim(tabledata[i].finished)=="0"?"已完成":"未完成"
                        div2.innerHTML = "交易状态：" + finish;
                        var div4 = document.createElement("div");
                        div4.innerHTML = "下单时间：" + tabledata[i].createtime.substring(0,10);
                        var div3 = document.createElement("div");
                        div3.style.color = "red";
                        div3.innerHTML = "数量：" + tabledata[i].amount;
                        var div5 = document.createElement("div");

                        var input0 = document.createElement("input");
                        input0.type = "button";
                        input0.id = "get" + $.trim(tabledata[i].id);
                        input0.onclick = function () { OrderPage.getpruduct(this.id); }
                        input0.value = "确认收货";
                        if ($.trim(tabledata[i].finished) == "0") {
                            input0.disabled = "true";
                        }
                        input0.className = "btn btn-default btn-sm";
                        var input1 = document.createElement("input");
                        input1.type = "button";
                        input1.onclick = function () { OrderPage.deleteproduct(this.id); }
                        input1.id = "delete" + $.trim(tabledata[i].id);
                        input1.value = "删除";
                        input1.className = "btn btn-default btn-sm";
                        div5.appendChild(input0);
                        div5.appendChild(input1);

                        td1.appendChild(div);
                        td1.appendChild(div1);
                        td1.appendChild(div2);
                        td1.appendChild(div4);
                        td1.appendChild(div3);
                        td1.appendChild(div5);
                        tr.appendChild(td);
                        tr.appendChild(td1);
                        tableactive_.appendChild(tr);
                    }
                }
        }
        else {
            layer_alert_error(data.msg);
        }
    },
    getpruduct: function (id) {
        OrderPage.sendid = id;
        OrderPage.sendtype = 1;
        //询问框
        layer.confirm('该商品确定收货？', {
            btn: ['确定', '取消'] //按钮
        }, function (index) {
            OrderPage.sendhandle();
            layer.close(index);
        }, function () {
            layer.close(index);
        });
    },
    deleteproduct: function (id) {
        OrderPage.sendid = id;
        OrderPage.sendtype = 0;
        //询问框
        layer.confirm('该商品确定删除？', {
            btn: ['确定', '取消'] //按钮
        }, function (index) {
            OrderPage.sendhandle();
            layer.close(index);
        }, function (index) {
            layer.close(index);
        });
    },
    sendhandle: function () {
        var openid = $.cookie("openid");
        if (isUndefinedNullOrEmpty(openid)) {
            alert("服务器获取数据失败请稍候再试");
            return false;
        }
        if (OrderPage.sendtype == 1) {
            var id = OrderPage.sendid.replace("get", "");
            var sql = " update Orders_Record set finished='0' where id='"+id+"' ";
            var content = sql + "|" + openid;
            poststr = "{\"type\":\"orderpagexiugai\",\"htmldata\":\"" + content + "\"}";
            this.postajaxs(poststr, this.showdata);
            
        }
        else if (OrderPage.sendtype == 0) {
            var id = OrderPage.sendid.replace("delete", "");
            var sql = " update Orders_Record set deletes='0' where id='" + id + "' ";
            var content = sql + "|" + openid;
            poststr = "{\"type\":\"orderpagexiugai\",\"htmldata\":\"" + content + "\"}";
            this.postajaxs(poststr, this.showdata);
            
        }
    },
    postajaxs: function (postdata, fangfa) {
        mj_ajax("../Handler1.ashx", "json", postdata, fangfa)
    }
}